﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web.backend
{
    public partial class adminList : BasePage
    {

        WTODataContext contex = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            AspNetPager1.RecordCount = contex.admins.Count();
            var list = from c in contex.admins orderby c.created_date descending select c;
            psd.DataSource = list.ToList();
            psd.AllowPaging = true;
            psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            psd.PageSize = AspNetPager1.PageSize;

            this.rptLinks.DataSource = psd;
            this.rptLinks.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptLinks_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var item = e.Item.DataItem;
                admin cat = item as admin;

                LinkButton button = e.Item.FindControl("btnDelete") as LinkButton;
                Label recordNo = e.Item.FindControl("lblNo") as Label;
                Label roleText = e.Item.FindControl("lblRole") as Label;

                recordNo.Text = (e.Item.ItemIndex + 1 + (AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1))).ToString();
                switch (cat.admin_type)
                {
                    case "1": roleText.Text = "超级管理员"; break;
                    case "2": roleText.Text = "外商服务中心"; break;
                    case "3": roleText.Text = "人才培训中心"; break;
                    default: roleText.Text = ""; break;
                }
                button.Attributes.Add("onclick", "return confirm('您确定要删除该条管理员记录吗？');");
            }
        }
        protected void rptLinks_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteCate")
            {
               
                int link_id = Convert.ToInt32(e.CommandArgument);
                admin cat = contex.admins.SingleOrDefault(p => p.serial_number == link_id);
                if (cat != null)
                {
                    if (cat.admin_type == "1")
                    {
                        var admins = contex.admins.Where(p => p.admin_type == "1");
                        if (admins == null)
                        {
                            return;
                        }
                        else
                        {
                            if (admins.Count() <= 1)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "fau", "alert('警告：不能删除数据库中最后一个系统管理员!');", true);
                                return;
                            }

                        }
                    }
                    contex.admins.DeleteOnSubmit(cat);
                    contex.SubmitChanges();
                }
                BindGrid();
            }
        }
    }
}